package cn.ht.jywrwater.module.ht.controller.admin.village;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.village.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.village.VillageDO;
import cn.ht.jywrwater.module.ht.convert.village.VillageConvert;
import cn.ht.jywrwater.module.ht.service.village.VillageService;

@Tag(name = "管理后台 - 村庄")
@RestController
@RequestMapping("/ht/village")
@Validated
public class VillageController {

    @Resource
    private VillageService villageService;

    @PostMapping("/create")
    @Operation(summary = "创建村庄")
    @PreAuthorize("@ss.hasPermission('ht:village:create')")
    public CommonResult<Long> createVillage(@Valid @RequestBody VillageCreateReqVO createReqVO) {
        return success(villageService.createVillage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新村庄")
    @PreAuthorize("@ss.hasPermission('ht:village:update')")
    public CommonResult<Boolean> updateVillage(@Valid @RequestBody VillageUpdateReqVO updateReqVO) {
        villageService.updateVillage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除村庄")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:village:delete')")
    public CommonResult<Boolean> deleteVillage(@RequestParam("id") Long id) {
        villageService.deleteVillage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得村庄")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:village:query')")
    public CommonResult<VillageRespVO> getVillage(@RequestParam("id") Long id) {
        VillageDO village = villageService.getVillage(id);
        return success(VillageConvert.INSTANCE.convert(village));
    }

    @GetMapping("/list")
    @Operation(summary = "获得村庄列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:village:query')")
    public CommonResult<List<VillageRespVO>> getVillageList(@RequestParam("ids") Collection<Long> ids) {
        List<VillageDO> list = villageService.getVillageList(ids);
        return success(VillageConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得村庄分页")
    @PreAuthorize("@ss.hasPermission('ht:village:query')")
    public CommonResult<PageResult<VillageRespVO>> getVillagePage(@Valid VillagePageReqVO pageVO) {
        PageResult<VillageDO> pageResult = villageService.getVillagePage(pageVO);
        return success(VillageConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出村庄 Excel")
    @PreAuthorize("@ss.hasPermission('ht:village:export')")
    @OperateLog(type = EXPORT)
    public void exportVillageExcel(@Valid VillageExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<VillageDO> list = villageService.getVillageList(exportReqVO);
        // 导出 Excel
        List<VillageExcelVO> datas = VillageConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "村庄.xls", "数据", VillageExcelVO.class, datas);
    }

}
